package com.baidu.location;

import android.content.Context;
import android.os.Environment;
import android.os.Process;
import java.io.File;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpParams;

public final class ab
  implements Thread.UncaughtExceptionHandler
{
  private Context b;

  ab(f paramf, Context paramContext)
  {
    this.b = paramContext;
    while (true)
    {
      try
      {
        paramf = Environment.getExternalStorageDirectory().getPath() + "/traces";
        paramf = new File(paramf + "/error_fs.dat");
        if (paramf.exists())
        {
          RandomAccessFile localRandomAccessFile = new RandomAccessFile(paramf, "rw");
          localRandomAccessFile.seek(280L);
          if (1326 == localRandomAccessFile.readInt())
          {
            localRandomAccessFile.seek(308L);
            int i = localRandomAccessFile.readInt();
            if ((i <= 0) || (i >= 2048))
              break label269;
            ak.e();
            paramf = new byte[i];
            localRandomAccessFile.read(paramf, 0, i);
            paramf = new String(paramf, 0, i);
            localRandomAccessFile.seek(600L);
            i = localRandomAccessFile.readInt();
            paramContext = localObject;
            if (i > 0)
            {
              paramContext = localObject;
              if (i < 2048)
              {
                ak.e();
                paramContext = new byte[i];
                localRandomAccessFile.read(paramContext, 0, i);
                paramContext = new String(paramContext, 0, i);
              }
            }
            new StringBuilder().append(paramf).append(paramContext);
            ak.e();
            if (a(paramf, paramContext))
            {
              localRandomAccessFile.seek(280L);
              localRandomAccessFile.writeInt(12346);
            }
          }
          localRandomAccessFile.close();
        }
        return;
      }
      catch (java.lang.Exception paramf)
      {
        return;
      }
      label269: paramf = null;
    }
  }

  private void a(File paramFile, String paramString1, String paramString2)
  {
    try
    {
      paramFile = new RandomAccessFile(paramFile, "rw");
      paramFile.seek(280L);
      paramFile.writeInt(12346);
      paramFile.seek(300L);
      paramFile.writeLong(System.currentTimeMillis());
      byte[] arrayOfByte = paramString1.getBytes();
      paramFile.writeInt(arrayOfByte.length);
      paramFile.write(arrayOfByte, 0, arrayOfByte.length);
      paramFile.seek(600L);
      arrayOfByte = paramString2.getBytes();
      paramFile.writeInt(arrayOfByte.length);
      paramFile.write(arrayOfByte, 0, arrayOfByte.length);
      if (!(a(paramString1, paramString2)))
      {
        paramFile.seek(280L);
        paramFile.writeInt(1326);
      }
      paramFile.close();
      return;
    }
    catch (java.lang.Exception paramFile)
    {
    }
  }

  private boolean a(String paramString1, String paramString2)
  {
    if (!(ae.a(this.b)))
      return false;
    try
    {
      HttpPost localHttpPost;
      do
      {
        localHttpPost = new HttpPost(ak.e);
        ArrayList localArrayList = new ArrayList();
        localArrayList.add(new BasicNameValuePair("e0", paramString1));
        localArrayList.add(new BasicNameValuePair("e1", paramString2));
        localHttpPost.setEntity(new UrlEncodedFormEntity(localArrayList, "utf-8"));
        paramString1 = new DefaultHttpClient();
        paramString1.getParams().setParameter("http.connection.timeout", Integer.valueOf(12000));
        paramString1.getParams().setParameter("http.socket.timeout", Integer.valueOf(12000));
        ak.e();
      }
      while (paramString1.execute(localHttpPost).getStatusLine().getStatusCode() != 200);
      ak.e();
      return true;
    }
    catch (java.lang.Exception paramString1)
    {
    }
    return false;
  }

  public final void uncaughtException(Thread paramThread, Throwable paramThrowable)
  {
    Object localObject2;
    Object localObject1 = null;
    if (!(ak.ac))
    {
      Process.killProcess(Process.myPid());
      return;
    }
    try
    {
      paramThread = new StringWriter();
      localObject2 = new PrintWriter(paramThread);
      paramThrowable.printStackTrace((PrintWriter)localObject2);
      ((PrintWriter)localObject2).close();
      label295: label222: paramThread = paramThread.toString();
    }
    catch (java.lang.Exception paramThread)
    {
      try
      {
        ak.e();
        f.m(this.a);
        localObject2 = t.a(false);
        paramThrowable = (Throwable)localObject2;
        if (f.o(this.a) != null)
          paramThrowable = ((String)localObject2) + f.o(this.a).b();
        if (paramThrowable != null)
          paramThrowable = Jni.a(paramThrowable);
      }
      catch (java.lang.Exception paramThread)
      {
        try
        {
          long l;
          Object localObject3 = Environment.getExternalStorageDirectory().getPath() + "/traces";
          localObject2 = new File(((String)localObject3) + "/error_fs.dat");
          if (!(((File)localObject2).exists()))
          {
            localObject3 = new File((String)localObject3);
            if (!(((File)localObject3).exists()))
              ((File)localObject3).mkdirs();
            if (((File)localObject2).createNewFile())
              break label295;
            a((File)localObject1, paramThrowable, paramThread);
          }
          do
          {
            while (true)
            {
              Process.killProcess(Process.myPid());
              return;
              paramThread = paramThread;
              paramThread = null;
              paramThrowable = null;
            }
            localObject1 = new RandomAccessFile((File)localObject2, "rw");
            ((RandomAccessFile)localObject1).seek(300L);
            l = ((RandomAccessFile)localObject1).readLong();
          }
          while (System.currentTimeMillis() - l <= 604800000L);
          a((File)localObject2, paramThrowable, paramThread);
        }
        catch (java.lang.Exception paramThread)
        {
          while (true)
          {
            while (true)
            {
              while (true)
              {
                break label222:
                paramThrowable = paramThrowable;
              }
              localObject1 = localObject2;
            }
            paramThrowable = null;
          }
        }
      }
    }
  }
}